package com.gxa.learning.pc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.pojo.Course;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.pc.mapper.CourseMapper;
import com.gxa.learning.pc.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * 课程的服务层实现类
 *
 * @Author ChenJ
 * @date 2021/12/03 11:18
 **/
@Service("courseService")
public class CourseServiceImpl extends BaseServiceImpl<Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageInfo<Course> selectTrueList(Integer page, Integer size, Course course) {
        // 启用分页
        PageHelper.startPage(page, size);

        List<Course> list = null;

        if (course != null) {
            // 创建模糊得example实体类
            Example example = new Example(Course.class);
            // 模糊规则
            Example.Criteria criteria = example.createCriteria();
            Boolean status = course.getStatus();
            if (status != null) {
                if (status) {
                    criteria.andEqualTo("status", "1");
                } else {
                    criteria.andEqualTo("status", "0");
                }
            }
            if (course.getTitle() != null) {
                criteria.andLike("title", "%" + course.getTitle() + "%");
            }
            if (course.getTypeId() != null) {
                criteria.andLike("typeId", course.getTypeId().toString());
            }
            if (course.getLmId() != null) {
                criteria.andLike("lmId", course.getLmId().toString());
            }
            if (course.getAdmin() != null) {
                criteria.andLike("admin", "%" + course.getAdmin() + "%");
            }
            list = courseMapper.selectByExample(example);
        } else {
            list = courseMapper.selectAll();
        }

        return PageInfo.of(list);
    }

    /**
     * 根据编号获取类别和等级
     *
     * @param id 课程编号
     * @return
     */
    @Override
    public Map<String, String> getTypeAndLevel(Long id) {

        Map<String, String> typeAndLevel = courseMapper.getTypeAndLevel(id);
        return typeAndLevel;
    }

}
